﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bias_Stock.Models;
using WinNemoLib;

namespace Bias_Stock.Controllers
{
    public class CartCountController : Controller
    {
        //
        // GET: /CartCount/

        [Authorize]
        public ActionResult Index()
        {
            
            return View();
        }

        public ActionResult Login()
        {
            return View();
        }

        [Authorize]
        public ActionResult Count()
        {
            return View();
        }

        [Authorize]
        public ActionResult Cart_Tag(string area)
        {
            ViewBag.area = area.ToUpper();
            return View();
        }

         [Authorize]
        public ActionResult Count_Cart(string area)
        {
            CountCartModel.data_ds data = new CountCartModel.data_ds();
            data._area = area.ToUpper();
            return View(data);
        }

         [Authorize]
        public ActionResult Count_Tag(string area, string cart,bool rep = false)
        {
            CountCartModel.data_ds data = new CountCartModel.data_ds();
            data._area = area.ToUpper();
            data._cart = cart;
            if (!rep)
            {
                CountCartModel m = new CountCartModel();
                List<CountCartModel.Cart_CNT_DS> list = m.List_Cart_Count(cart);
                if (list.Count > 0)
                {  
                    return RedirectToAction("Exit_data", new { area = area, cart = cart });
                }
            }
            return View(data);
        }

         [HttpPost]
         public string EncodePass(string pw)
         {
             return LoginAuthorize.EnCodeMD5.MD5CalcString(pw);
         }

        [HttpPost]
        public string GetCartno(string tag)
        {
            string[] arr = tag.ToUpper().Split(new string[] { "+" }, StringSplitOptions.None);

           CountCartModel  d = new CountCartModel();
            List<CountCartModel.PlyRoll_DS> list = new List<CountCartModel.PlyRoll_DS>();
            string[] dPlan = arr[9].Split(new string[] { "-" }, StringSplitOptions.None);
            list = d.SEL_TAG(arr[8], dPlan[0], dPlan[1], dPlan[2], arr[1], arr[7],false);
            if (list.Count > 0)
            {
                return list[0]._cartno;
            }
            return "";
        }

        [HttpPost]
        public string GetTag(string tag)
        {
            string[] arr = tag.ToUpper().Split(new string[] { "+" }, StringSplitOptions.None);
            if (arr.Length != 10)
            {
                return " Tag not found! ";
            }

            CountCartModel d = new CountCartModel();
            List<CountCartModel.Cart_CNT_DS> list = new List<CountCartModel.Cart_CNT_DS>();
            list =d.List_Count("", true, "", "", tag);
            if (list.Count > 0)
            {
                return "Tag is used !";
            }
            return "";
        }

         [Authorize]
        public ActionResult Tag_Cart(string area ,string tag,string cartno)
        {
            //HN-38MH+L+2.50-17NR74/L+STB+15/08-1-STB+26/08/2013+50+1+3+25/02/2014-10-1
            string[] arr = tag.Split(new string[] { "+" }, StringSplitOptions.None);
            CountCartModel  d = new CountCartModel();
       
            string[] dPlan = arr[9].Split(new string[] { "-" }, StringSplitOptions.None);    
            List<CountCartModel.PlyRoll_DS> list = new List<CountCartModel.PlyRoll_DS>();
            list = d.SEL_TAG(arr[8], dPlan[0], dPlan[1], dPlan[2], arr[1], arr[7],true);
            list[0]._area = area;
            list[0]._tag = tag;
            list[0]._cartno = cartno;
            return View(list[0]);
        }

        public JsonResult Save_Cart(List<CountCartModel.PlyRoll_DS> thedata)
        {
            CountCartModel mdSearch = new CountCartModel();
            using (ConnectionWrapper cnn = Nemo.GetDefaultConfiguredDbConnectionWrapper())
            {
                cnn.Open();
                cnn.BeginTransaction();
                string usernm = User.Identity.Name.ToString();

                mdSearch.Del_Data_Cart(cnn, thedata[0]._cartno);
              
                if (mdSearch.Ins_Data_Cart(cnn, thedata[0], usernm))
                {
                    cnn.CommitTransaction();
                    return Json(new { success = 1, message = "" });

                }
                else
                {                    
                    return Json(new { success = 0, message = "ไม่สามารถบันทึกได้" });
                }
            }

        }

        public JsonResult Blank_Cart(List<CountCartModel.PlyRoll_DS> thedata,string cartno)
        {
            CountCartModel mdSearch = new CountCartModel();

            using (ConnectionWrapper cnn = Nemo.GetDefaultConfiguredDbConnectionWrapper())
            {
                cnn.Open();
                cnn.BeginTransaction();
                string usernm = User.Identity.Name.ToString();
                thedata[0]._cartno = cartno;
                mdSearch.Del_Data_Cart(cnn, thedata[0]._cartno);

                if (mdSearch.Ins_Data_Blank(cnn, thedata[0],usernm))
                {
                    cnn.CommitTransaction();
                    return Json(new { success = 1, message = "" });

                }
                else
                {                    
                    return Json(new { success = 0, message = "ไม่สามารถบันทึกได้" });
                }
            }

        }

         [Authorize]
        public ActionResult Exit_data(string area,string cart,string tag)
        {
            SessionBag.Current.area = area;
            CountCartModel m = new CountCartModel();
            List<CountCartModel.Cart_CNT_DS> list = m.List_Count(cart);
            list[0]._tag = tag;
            return View(list[0]);
        }

         [HttpPost]
         public string CartInDB(string cartno,Boolean notuse=false)
         {
             MasterModel d = new MasterModel();
              List<MasterModel.CartMast_DS> list = d.SEL_Cart_MASTER(cartno,notuse);
              if (list.Count <= 0) return "";
             return "1";
         }

         [HttpPost]
         public JsonResult CartInDB_Count(string cartno,Boolean notuse=false)
         {
             MasterModel d = new MasterModel();
              List<MasterModel.CartMast_DS> list = d.SEL_Cart_MASTER(cartno,notuse);
              if (list.Count <= 0)
              {        //cart no use         
                  return Json(new { success = 0, message = "Cart no." +cartno +" is not Used" });
              }
              CountCartModel m = new CountCartModel();
              List<CountCartModel.Cart_CNT_DS> db = m.List_Count(cartno);

              if (db.Count <= 0)
              {
                  return Json(new { success = 1, message = "" });
              }

              return Json(new { success = 2, message = "Cart no." + cartno + " is used" });
         }

        [Authorize]
        public ActionResult Del_Cart(string cartno){
            CountCartModel m = new CountCartModel();
            List<CountCartModel.Cart_CNT_DS> list = m.List_Count(cartno);
            if (list.Count > 0)
            {
                return View(list[0]);
            }
            return View(new CountCartModel.Cart_CNT_DS());
        }

        [HttpPost]
        public JsonResult Delete_Cart(string cartno)
        {
            CountCartModel mdSearch = new CountCartModel();
            using (ConnectionWrapper cnn = Nemo.GetDefaultConfiguredDbConnectionWrapper())
            {
                cnn.Open();
                cnn.BeginTransaction();                           

             
                if (mdSearch.Del_Data_Cart(cnn, cartno))
                {
                    cnn.CommitTransaction();
                    return Json(new { success = 1, message = "" });
                }
                else
                {
                    return Json(new { success = 0, message = "ไม่สามารถบันทึกได้" });
                }
            }
        }

        // ############## QUERY ################
         [Authorize]
        public ActionResult Query_Cart()
        {
            return View();
        }
         [Authorize]
        public ActionResult Query_Size(string size)
        {
            SessionBag.Current.Size = size.ToUpper();
            CountCartModel m = new CountCartModel();
            List<CountCartModel.Cart_CNT_DS> list = m.List_Count("",true,size.ToUpper());
            if (list.Count <= 0)
            {
               list = new List<CountCartModel.Cart_CNT_DS>();
                return View(list);
            }
            return View(list);
        }
         [Authorize]
        public ActionResult Query_CartNo(string size,string cart)
        {
            CountCartModel m = new CountCartModel();
            List<CountCartModel.Cart_CNT_DS> list = m.List_Count(cart,true,size);
            if (list.Count <= 0)
            {
               return View(new CountCartModel.Cart_CNT_DS());
            }
            return View(list[0]);
        
        }



    }
}
